
//Table 7 final run

log using "$rootpath/log/Table_7_Results.log", replace
eststo clear
use "$rootpath/processed_data/Estimation_Data.dta", clear

sort FIRM_DUNS year
egen mean_treat=mean(avg_old_code_prob), by(FIRM_DUNS year)
egen mean_treat_ever=mean(avg_old_code_prob), by(FIRM_DUNS)
gen firm_diff=avg_old_code_prob - mean_treat_ever
egen firm_year=group(FIRM_DUNS year)
replace firm_year=0 if firm_year==.
replace mean_treat=treatXpost if no_parent_match==1
gen sub_treat=treatXpost - mean_treat*post
 
gen sub_treatXtreatXpost = sub_treat*treatXpost
sort facility_id year
//facility-level se
reghdfe  log_damage55 sub_treatXtreatXpost treatXpost sub_treat if key_estimation_sample==1, absorb(facility_id n5#year firm_year) vce(cluster facility_id)
estimates store ddd_1_damage

//state-level se
reghdfe  log_damage55 sub_treatXtreatXpost treatXpost sub_treat if key_estimation_sample==1, absorb(facility_id n5#year firm_year) vce(cluster state_number)
estimates store ddd_1_damageSE
//facility-level se
reghdfe  log_emissions4 sub_treatXtreatXpost treatXpost sub_treat if key_estimation_sample==1, absorb(facility_id n5#year firm_year) vce(cluster facility_id)
estimates store ddd_1_emission

//state-level se
reghdfe  log_emissions4 sub_treatXtreatXpost treatXpost sub_treat if key_estimation_sample==1, absorb(facility_id n5#year firm_year) vce(cluster state_number)
estimates store ddd_1_emissionSE

sort FIRM_DUNS 
egen state_vals=nvals(state_number), by(FIRM_DUNS )
replace state_vals=1 if state_vals==.
replace state_vals=. if no_parent_match==1
//replace state_vals=1 if no_parent_match==1

sort facility_id year
//facility-level se
reghdfe  log_damage55  treatXpost if key_estimation_sample==1&state_vals==1&no_parent_match==0, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store ddd_2_damage

//state-level se
reghdfe  log_damage55  treatXpost if key_estimation_sample==1&state_vals==1&no_parent_match==0, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_2_damageSE
//This is the check for “ results for plants in high treatment states 

//facility-level se
reghdfe  log_damage55  treatXpost if key_estimation_sample==1&firm_diff>=0&no_parent_match==0, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store ddd_3_damage

//state-level se
reghdfe  log_damage55  treatXpost if key_estimation_sample==1&firm_diff>=0&no_parent_match==0, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_3_damageSE
///This is the emissions based results

//facility-level se
reghdfe  log_emissions4  treatXpost if key_estimation_sample==1&state_vals==1&no_parent_match==0, absorb(facility_id n5#year) vce(cluster facility_id)
//This is the check for “ results for plants in high treatment states 

estimates store ddd_2_emission

//state-level se
reghdfe  log_emissions4  treatXpost if key_estimation_sample==1&state_vals==1&no_parent_match==0, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_2_emissionSE
//facility-level se
reghdfe  log_emissions4  treatXpost if key_estimation_sample==1&firm_diff>=0&no_parent_match==0, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store ddd_3_emission

//state-level se
reghdfe  log_emissions4  treatXpost if key_estimation_sample==1&firm_diff>=0&no_parent_match==0, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_3_emissionSE
esttab ddd_1_damage ddd_2_damage ddd_3_damage ddd_1_emission ddd_2_emission ddd_3_emission using "$rootpath/table_graph/DDD_TBL7.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(sub_treatXtreatXpost treatXpost sub_treat _cons) scalars(N F) ar2 replace 
esttab ddd_1_damageSE ddd_2_damageSE ddd_3_damageSE ddd_1_emissionSE ddd_2_emissionSE ddd_3_emissionSE using "$rootpath/table_graph/DDD_TBL7SE.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(sub_treatXtreatXpost treatXpost sub_treat _cons) scalars(N F) ar2 replace 
log close
